package cn.yqn.org.service.impl;

import cn.yqn.basic.service.impl.BaseServiceImpl;
import cn.yqn.org.domain.Department;
import cn.yqn.org.mapper.DepartmentMapper;
import cn.yqn.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        String dirPath = "";
        if (department.getParent().getId() == null){
            dirPath = "/" + department.getId();
        }else {
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getSn() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        String dirPath = "";
        if (department.getParent().getId() == null){
            dirPath = "/" + department.getId();
        }else {
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getSn() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    public List<Department> Tree() {
//        // 查询全部数据
//        List<Department> depts = departmentMapper.loadAll();
//        // 创建一个集合来存放父级部门
//        List<Department> deptTree = new ArrayList<>();
//        // 查询数据中的一级部门,循环遍历判断parent_id没有上级为一级部门
//        for (Department deptTmp : depts) {
//            if (deptTmp.getParent_id() == null) {
//                // 如果没有上级部门那么存入集合
//                deptTree.add(deptTmp);
//            } else {
//                // 判断非一级父类部门
//                for (Department parent : depts) {
//                    if (parent.getId() == deptTmp.getParent_id()) {
//                        parent.getChildren().add(deptTmp);
//                        break;
//                    }
//                }
//            }
//        }
//        return deptTree;
        // 查出全部数据
        List<Department> depts = departmentMapper.loadAll();
        // 创建对象收集数据
        List<Department> deptTree = new ArrayList<Department>();
        HashMap<Long, Department> deptMap = new HashMap<>();
        for (Department department : depts) {
            deptMap.put(department.getId(), department);
        }
        // 收集一级数据
        for (Department deptTmp : depts) {
            if(deptTmp.getParent_id() == null){
                deptTree.add(deptTmp);
            }else {
                // 对应一级数据的下级数据
                Department department = deptMap.get(deptTmp.getParent_id());
                if (department != null){
                    department.getChildren().add(deptTmp);
                }
            }
        }
        // 返回数据信息
        return deptTree;
      }
}
